package data_structure.test;

import org.w3c.dom.NodeList;
import sun.dc.pr.PRError;

class SingleLinkedListDemo {
    public static void main(String[] args) {
        Student s1 = new Student(1,"张三");
        Student s2 = new Student(2,"李四");
        Student s3 = new Student(3,"王五");
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addList(s1);
        singleLinkedList.addList(s2);
        singleLinkedList.addList(s3);
        singleLinkedList.selectList();
        System.out.println();
        Student s4 = new Student(2,"隔壁老王");
        singleLinkedList.updateList(s4);
        singleLinkedList.selectList();
        System.out.println("====删除结点====");
        singleLinkedList.deleteList(1);
        singleLinkedList.deleteList(2);
        singleLinkedList.deleteList(3);
        singleLinkedList.selectList();
    }
}

class SingleLinkedList{
    //头结点
    private Student head = new Student(0,"");

    //删除结点
    public void deleteList(int id){
        Student temp = head;
        while (true){
            if (temp.next == null) {
                System.out.println("没有找到该结点");
                return;
            }
            if (temp.next.getStuId() == id){
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;
        }
    }

    //修改节点的信息，根据no编号来修改，no编号不能修改
    //1.根据 newStudent 的 no 来修改即可
    public void updateList(Student student){
        //判断是否为空
        if (head.next == null){
            System.out.println("链表为空~");
            return;
        }
        //找到需要修改的节点，根据no进行编号
        //定义一个辅助变量
        Student temp = head.next;
        boolean flag = false;
        while (true){
            if (temp == null){
                break; //已经遍历完链表
            }
            if (temp.getStuId() == student.getStuId()){
                //找到
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //根据flag判断是否找到要修改节点
        if (flag){
            temp.setName(student.getName());
        }else
            System.out.println("没有找到该节点~");
    }

    //添加信息
    public void addList(Student student){
        //备份 头结点
        Student temp = head;
        while (true){
            if (temp.next == null)
                break;
            temp = temp.next;
        }
        temp.next = student;
    }

    //遍历
    public void selectList(){
        if (head.next == null){
            System.out.println("链表为空...");
            return;
        }
        Student temp = head.next ;
        while (true){
            if (temp == null)
                break;
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

class Student{
    private int stuId;
    private String name;
    Student next;

    public Student(int stuId, String name) {
        this.stuId = stuId;
        this.name = name;
    }

    public int getStuId() {
        return stuId;
    }

    public void setStuId(int stuId) {
        this.stuId = stuId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuId=" + stuId +
                ", name='" + name + '\'' +
                '}';
    }
}